package com.bingxue.edu.management.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.bingxue.edu.management.auth.EmployeeAuthUtil;
import com.bingxue.edu.management.system.model.query.OnlineUserQuery;
import com.bingxue.edu.management.system.model.resp.OnlineUserResp;
import com.bingxue.edu.management.system.service.OnlineUserService;
import com.bingxue.edu.framework.common.model.PageQuery;
import com.bingxue.edu.framework.common.model.Pagination;
import com.bingxue.edu.framework.common.model.Response;
import com.bingxue.edu.framework.common.util.ValidationUtils;
import com.bingxue.edu.framework.security.ApiResource;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author panbo
 * @since 2024/8/27
 */
@Tag(name = "在线用户管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/system/monitor/online")
public class OnlineUserController {

    private final OnlineUserService onlineUserService;

    @Operation(summary = "分页查询列表", description = "分页查询列表")
    @SaCheckPermission("system:online-user:manage")
    @ApiResource("system:online-user:manage")
    @GetMapping("/list")
    public Response<Pagination<OnlineUserResp>> page(OnlineUserQuery query, @Validated PageQuery pageQuery) {
        return Response.success(onlineUserService.page(query, pageQuery));
    }

    @Operation(summary = "强退在线用户", description = "强退在线用户")
    @Parameter(name = "token", description = "令牌", example = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOjEsInJuU3RyIjoiTUd6djdyOVFoeHEwdVFqdFAzV3M5YjVJRzh4YjZPSEUifQ.7q7U3ouoN7WPhH2kUEM7vPe5KF3G_qavSG-vRgIxKvE", in = ParameterIn.PATH)
    @SaCheckPermission("monitor:online:kickout")
    @ApiResource("system:online-user:manage")
    @PostMapping("/kickout/{token}")
    public Response<Void> kickout(@PathVariable String token) {
        String currentToken = EmployeeAuthUtil.STP.getTokenValue();
        ValidationUtils.throwIfEqual(token, currentToken, "不能强退自己");
        EmployeeAuthUtil.STP.kickoutByTokenValue(token);
        return Response.success();
    }
}

